package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.Video;
import com.ktjy.ch15.mapper.VideoMapper;
import com.ktjy.ch15.service.VideoService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("videoService")
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public Page<Video> findPage(Video video, Integer pageNo, Integer pageSize) {
        Page<Video> page = new Page<Video>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = videoMapper.count(video);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<Video> data = videoMapper.getPage(video, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(Video video) {
        return videoMapper.insert(video) > 0;
    }

    @Override
    public Video findById(Long id) {
        return videoMapper.getById(id);
    }

    @Override
    public boolean update(Video video) {
        return videoMapper.update(video) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return videoMapper.delete(id) > 0;
    }

    @Override
    public Video findByTitle(String title) {
        return videoMapper.getByTitle(title);
    }
}
